package DTaskBugzillaApp::Controller::Bugzilla;
use Mojo::Base 'Mojolicious::Controller';
use 5.018;
use warnings;

use BugzillaJsonRpc::Bug;
use DTask::Task;
use Tower::API;
use Data::Dumper;
use Mojo::JSON qw(true false);
use Mango;
use DTask::Utils qw(renderJsonResponse);
my $mango = Mango->new;

sub attachments
{
	my $c = shift;
	my $bug_id = $c->stash('bug');
	BugzillaJsonRpc::Bug::attachments($bug_id, sub {
		my ($ret) = @_;
		if ($ret->{error})
		{
			warn Dumper $ret->{error_message};
			$c->render(json => $ret);
			#ERROR EXIT
		}
		else
		{
			warn Dumper $ret->{result};
		}
	});
}


sub ExportToTowerTodo
{
	my $c = shift;
	my $bug_id = $c->stash('bug_id');

	my $headers = $c->tx->req->headers;
	my $todolist_guid = $c->stash('todolist_guid');
	my $access_token = $headers->authorization;

	DTask::Task::setMango($mango);

	DTask::Task::getTowerTodosByLink('bugzilla', $bug_id, sub{
		my (@tower_todo_guid) = @_;
		if (@tower_todo_guid)
		{
			$c->render(json => {error => false, result => [ @tower_todo_guid] });
			renderJsonResponse($c, [@tower_todo_guid]);

		}
		else
		{
			_export_bug_to_tower_todo($access_token, $bug_id, $todolist_guid, sub {
				renderJsonResponse($c, @_);
			});
		}
	
	});
}

sub _export_bug_to_tower_todo
{
	my ($access_token, $bug_id, $todolist_guid, $cb) = @_;
	my $delay = Mojo::IOLoop->delay(
		sub {
			my ($delay) = @_;
			my $end = $delay->begin;
			BugzillaJsonRpc::Bug::get($bug_id, sub {
				my ($ret) = @_;
				if ($ret->{error})
				{
					$delay->pass($ret);
					$end->();
				}
				else
				{
					my $bug_info = $ret->{result};
					warn Dumper $bug_info;
					my $title = "#bugzilla #$bug_id ". $bug_info->{summary};
					$delay->pass({
							error => false,
							title => $title,
							todo_comment => "from: <a href=\"https://bugzilla.deepin.io/show_bug.cgi?id=$bug_id\">bugzilla $bug_id</a>"
						});
					$end->();
				}
					
			});
		},
		sub {
			my ($delay, $info) = @_;

			if ($info->{error})
			{
				$delay->pass($info);
				return;
			}
			
			my $end = $delay->begin;
			Tower::API::addTodo($access_token, $todolist_guid, $info->{title}, undef, undef,sub {
				my ($ret) = @_;
				if ($ret->{error})
				{
					$delay->pass($ret);
					$end->();
				}
				else
				{
					my $result = $ret->{result};
					warn 'add todo done, dump ret result';
					warn Dumper $result;

					my $todo_guid = $result->{guid};
					$info->{todo_guid} = $todo_guid;

					my $project_guid = $result->{project}{guid};
					$info->{todo_url} = "https://tower.im/projects/$project_guid/todos/$todo_guid";

					$info->{bz_comment} = "Export to tower by dtask\n".
						$result->{project}{name}. ' :: ' . $result->{todolist}{name} . "\n". $info->{todo_url};

					DTask::Task::newWithTowerTodo($todo_guid, sub {
						my ($task_id) = @_;
						DTask::Task::linkWith($task_id, 'bugzilla', $bug_id, sub {
							$delay->pass($info);
							$end->();	
						});
					});

				}
			});
		},
		sub {
			my ($delay, $info) = @_;

			if ($info->{error})
			{
				$delay->pass($info);
				return;
			}

			warn 'step 3 dump $info';
			warn Dumper $info;
			my $end1 = $delay->begin;
			Tower::API::addTodoComment($access_token, $info->{todo_guid}, $info->{todo_comment}, sub {
				warn 'add todo comment done';	
				$delay->pass($info);
				$end1->();
			});
			BugzillaJsonRpc::Bug::update($bug_id,
				{
					url => $info->{todo_url},
					comment => {body => $info->{bz_comment} }
				}, $delay->begin );
		}
	);

	$delay->on(finish => sub {

		my ($delay, $info) = @_;
		if ($info->{error})
		{
			warn "_export_bug_to_tower_tod: on finish : error : ". $info->{error_message};
			$cb->(undef, $info->{error_message});
		}
		else
		{

			my $todo_url = $info->{todo_url};
			say "_export_bug_to_tower_todo: on finish : todo url is $todo_url";
			$cb->($todo_url);
		}
	});

}


1;
